package com.sw.server.handthreadpool;

import com.sw.servlet.HttpServlet;
import com.sw.servlet.Request;
import com.sw.servlet.Response;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

/**
 * @author sw
 * @project minicat
 * @description
 * @date 2020/4/30 09:16
 */
public class RequestProcess implements Runnable{

    private Socket socket;
    private Map<String, HttpServlet> servletMap;

    public RequestProcess(Socket socket, Map<String, HttpServlet> servletMap) {
        this.socket = socket;
        this.servletMap = servletMap;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());
            if (servletMap.get(request.getUrl()) == null) {//静态资源
                response.outPut(request.getUrl());
            } else {//动态资源
                HttpServlet httpServlet = servletMap.get(request.getUrl());
                httpServlet.service(request, response);
            }
            socket.close();
        }catch (IOException ioException){

        }
    }
}
